package moldat.utilities;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import moldat.ApplicationConfig;

/**
 * Importiert gen synonyme
 *
 * @author Sebastian Horwege
 */
public class GeneNameParser {

    private static File geneNameFile = null;
    private static Map<String, List<String>> geneSynonyms = null;

    public static void loadSynonyms() throws FileNotFoundException, IOException {
        if (geneSynonyms == null)
            geneSynonyms = new HashMap<String, List<String>>();
        geneNameFile = ApplicationConfig.getGeneNameFile();

        if (!geneNameFile.exists())
            return;

        BufferedReader in = null;
        String name = null;
        try {
            in = new BufferedReader(new FileReader(geneNameFile));
            List<String> synonyms = new LinkedList<String>();
            //Skip first line
            in.readLine();
            String line = in.readLine();
            do {
                String[] content = line.split("\\t");
                if (content.length == 2) {
                    name = content[0];
                    synonyms = new LinkedList<String>();
                    synonyms.addAll(Arrays.asList(content[1].split(", ")));
                    geneSynonyms.put(name, synonyms);
                }
                line = in.readLine();
            } while (line != null);
        } finally {
            if (in != null)
                in.close();
        }
    }

    public static List<String> getSynonyms(String geneName) throws FileNotFoundException, IOException {
        if (geneSynonyms == null)
            loadSynonyms();

        List<String> synonyms = geneSynonyms.get(geneName);
        if (synonyms == null)
            return new LinkedList<String>();
        return synonyms;
    }
}